/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.persistence;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.io.Serializable;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * The entity represents database objects bundle. Each bundle can be assigned to
 * {@link SourceNode} and have
 * 
 * @author Izzet_Mustafayev
 * 
 */
@Entity
@Table(name = "t_bundles")
@NamedQueries( {
		@NamedQuery(name = Bundle.FIND_BY_NAME, query = "FROM Bundle b WHERE b.name=:bundleName"),
		@NamedQuery(name = Bundle.FIND_BY_ID, query = "FROM Bundle b WHERE b.identity=:identity") })
public class Bundle implements Serializable {

	/**
	 * The constant for the find by name named query.
	 */
	public static final String FIND_BY_NAME = "findByName";

	/**
	 * The constant for the find by ID named query.
	 */
	public static final String FIND_BY_ID = "findByID";

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 10938401701327041L;

	/**
	 * The bundle identity.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private Long identity;

	/**
	 * The bundle name.
	 */
	private String name;

	/**
	 * The bundle {@link DBUnit}s set.
	 */
	@OneToMany(mappedBy = "bundle")
	private Set<DBUnit> dbunits;

	/**
	 * The assigned bundle's {@link SourceNode}.
	 */
	@ManyToOne
	@JoinColumn(name = "sourcenode_id", nullable = false)
	private SourceNode sourceNode;

	/**
	 * The source node last modifier user.
	 */
	@ManyToOne
	@JoinColumn(name = "modifier_id")
	private User modifier;

	/**
	 * The source node modification time.
	 */
	@Column(name = "updated_on")
	@Temporal(TemporalType.TIMESTAMP)
	private Date updatedOn;

	/**
	 * @return the identity
	 */
	public Long getIdentity() {
		return identity;
	}

	/**
	 * @param identity
	 *            the identity to set
	 */
	public void setIdentity(final Long identity) {
		this.identity = identity;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(final String name) {
		this.name = name;
	}

	/**
	 * @return the dbunits
	 */
	public Set<DBUnit> getDbunits() {
		return dbunits;
	}

	/**
	 * @param dbunits
	 *            the dbunits to set
	 */
	public void setDbunits(final Set<DBUnit> dbunits) {
		this.dbunits = dbunits;
	}

	/**
	 * @return the sourceNode
	 */
	public SourceNode getSourceNode() {
		return sourceNode;
	}

	/**
	 * @param sourceNode
	 *            the sourceNode to set
	 */
	public void setSourceNode(final SourceNode sourceNode) {
		this.sourceNode = sourceNode;
	}

	/**
	 * @return the modifier
	 */
	public User getModifier() {
		return modifier;
	}

	/**
	 * @param modifier
	 *            the modifier to set
	 */
	public void setModifier(final User modifier) {
		this.modifier = modifier;
	}

	/**
	 * @return the updatedOn
	 */
	public Date getUpdatedOn() {
		return updatedOn;
	}

	/**
	 * @param updatedOn
	 *            the updatedOn to set
	 */
	public void setUpdatedOn(final Date updatedOn) {
		this.updatedOn = updatedOn;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append(identity).append(name)
				.toString();
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj instanceof Bundle == false) {
			return false;
		}

		if (this == obj) {
			return true;
		}

		Bundle rhs = (Bundle) obj;

		return new EqualsBuilder().append(identity, rhs.identity).append(name,
				rhs.name).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(11, 49).append(identity).append(name)
				.toHashCode();
	}
}
